﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace Aim2 {
    public class AnimatedSprite {
        AnimatedObject owner;

        private int framecount;
        private float timePerFrame;
        public int Frame { get; set; }

        private float totalElapsed;

        private bool oneShot;
        public bool StopOnLastFrame { get; set; }
        private bool RandomRotation;

        public bool Reversed { get; set; }

        public bool Paused { get; set; }

        public bool OnLastFrame() {
            return (Frame == framecount - 1 && !Reversed || Reversed && Frame == 0);
        }

        public void Reverse() {
            this.Reversed = !this.Reversed;
        }

        public void AddToTPF(float f) {
            timePerFrame += f;
        }

        public float GetTPF() {
            return timePerFrame;
        }

        public bool LoopReverse { get; set; }

        public AnimatedSprite(  AnimatedObject owner, 
                                int FrameCount, 
                                int FramesPerSec,
                                bool RandomRotation = false,
                                bool oneShot = false, 
                                bool stopOnLastFrame = false,
                                bool reverse = false,
                                bool loopReverse = false) {
            this.owner = owner;
            this.oneShot = oneShot;
            this.StopOnLastFrame = stopOnLastFrame;
            this.RandomRotation = RandomRotation;
            this.framecount = FrameCount;
            this.timePerFrame = (float)1 / FramesPerSec;
            this.Reversed = reverse;
            this.LoopReverse = loopReverse;
            Frame = 0;
            totalElapsed = 0;
            Paused = false;
        }

        public void UpdateFrame(float elapsed) {
            if (Paused)
                return;
            totalElapsed += elapsed;

            if (totalElapsed > timePerFrame) {
                if (!Reversed)
                    Frame++;
                else
                    Frame--;
                totalElapsed -= timePerFrame;
                if (Frame >= framecount && !Reversed || Reversed && Frame < 0) {
                    if (oneShot) {
                        this.Stop();
                    }
                    else if (StopOnLastFrame) {
                        if (!Reversed)
                            Frame--;
                        else
                            Frame++;
                        Paused = true;
                    }
                    else {
                        if (LoopReverse)
                            this.Reverse();

                        if (!Reversed)
                            Frame = 0;
                        else
                            Frame = framecount - 1;

                        if (RandomRotation) {
                            float circle = MathHelper.Pi * 2;
                            owner.Rotation += Game.Random.Next(0, (int)circle);
                            owner.Rotation = owner.Rotation % circle;
                        }
                    }
                }

            }
        }

        internal Rectangle SourceRect { get; set; }
        bool drawing;

        public void DrawFrame(  SpriteBatch Batch, 
                                Vector2 screenpos,
                                Texture2D spriteTexture){
            if (owner.Inactive)
                drawing = !drawing;
            else
                drawing = true;
            if (drawing) {
                SourceRect = TextureHelper.getTextureRectangle(spriteTexture.Name.GetLayout(), Frame, spriteTexture.Width, spriteTexture.Height);
                Batch.Draw(spriteTexture, screenpos, SourceRect, Color.White,
                owner.Rotation, new Vector2(SourceRect.Width / 2, SourceRect.Height / 2), owner.Scale, SpriteEffects.None, owner.Depth);
            }
        }

        public void Reset() {
            if (!Reversed ^ !StopOnLastFrame)
                Frame = 0;
            else
                Frame = framecount - 1;
            totalElapsed = 0f;
        }

        public void Stop() {
            Paused = true;
            Reset();
        }
    }
}
